package com.example.book.service;

import com.example.book.enums.BookStatusEnum;
import com.example.book.mapper.BookMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/7
 * Time: 15:08
 */
@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        // 1. 总记录数
        Integer count = bookMapper.count();
        // 2. 当前页的记录数
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 0) {
//                bookInfo.setStatusCN("删除");
//            } else if(bookInfo.getStatus() == 1) {
//                bookInfo.setStatusCN("可借阅");
//            } else {
//                bookInfo.setStatusCN("不可借阅");
//            }
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
//        PageResponse<BookInfo> response = new PageResponse<>();
//        response.setTotal(count);
//        response.setRecords(bookInfos);
        return new PageResponse<BookInfo>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookMapper.batchDeleteBook(ids);
    }
}
